package com.example.char05;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;

public class AntiVirusDao {
    private Context context;

    public AntiVirusDao(Context context) {
        this.context = context;
    }

    /**
     * ��ȡ��������Ϣ  ���û�л�ȡ�����ؿ�
     *
     * @param md5
     * @return
     */
    public static String checkVirus(String md5) {
        //Ĭ������£�û�л�ȡ��������Ϣ
        String result = null;
        //�������ݿ��·��
        final String path = "/data/data/com.example.administrator.myapplication/files/antivirus.db";
        final String query = "select desc from datable where md5=?";
        final String[] whereArgs = {md5};
        final File[] file=new File[1];
        //�����ݿ�
        SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READONLY);
        if (db == null) {
            //һ��������·����û���ҵ����ݿ��ļ�����assets�ļ����¿���
            new Thread(new Runnable() {

                @Override
                public void run() {
                    file[0] = AssetCopyUtil.copy1(App.getContext(), "antivirus.db", path, null);
                }
            }).start();
            if (file[0] != null) {
                db = SQLiteDatabase.openDatabase(file[0].getPath(), null, SQLiteDatabase.OPEN_READONLY);
            }
        }
        result = OpenRawTuple(db, query, whereArgs);
        return result;
    }

    private static String OpenRawTuple(SQLiteDatabase db, String query, String[] whereArgs) {
        String rst = "";
        if (db.isOpen()) {
            //ִ�в�ѯ����������һ�������
            Cursor cursor = db.rawQuery(query, whereArgs);
            if (cursor.moveToFirst()) {
                rst = cursor.getString(0);
            }
            //����ر�ϵͳ���α꣬���û�йرգ���ʹ�ر������ݿ⣬Ҳ���ױ����ڴ�й©���쳣��Ϣ
            cursor.close();
            db.close();
        }
        return rst;
    }
}
